﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AddressBookModel;
using Exceptions;

namespace AppControl
{
    /// <summary>
    /// Interaction logic for EditContactWindow.xaml
    /// </summary>
    public partial class EditContactWindow : Window
    {
        private Contact EditContact { get; set; }
        // get the other window elements
        private ContactsWindow contactsWindow = Application.Current.Windows.Cast<Window>().
            FirstOrDefault(window => window is ContactsWindow) as ContactsWindow;

        public EditContactWindow()
        {
            InitializeComponent();
            LoadComponents();
        }

        private void LoadComponents()
        {
            var index = contactsWindow.preview.SelectedIndex;

            try
            {
                EditContact = (Contact)(ContactsWindow.GroupedContacts as ListCollectionView).GetItemAt(index);
            }
            catch (InvalidCastException ex) { MessageBox.Show(ex.Message); }

            this.EditName.Text = EditContact.Name;
            this.EditMobile.Text = EditContact.PhoneNumber;
            this.EditEmail.Text = EditContact.Email;
            if (EditContact.Birthday.HasValue)
            {
                this.EditBirthday.Text = EditContact.Birthday.Value.Date.ToShortDateString();
            }
            this.GroupType.SelectedIndex = (int)EditContact.Group;
        }

        private void OnSaveBtnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                Contact.ValidateContact(this.EditName.Text, this.EditMobile.Text, this.EditBirthday.Text);

                if (this.GroupType.SelectionBoxItem.ToString() == EditContact.Group.ToString())
                {
                    DateTime date;
                    bool isDate = DateTime.TryParse(this.EditBirthday.Text.Trim(), out date);

                    EditContact.Name = this.EditName.Text.Trim();
                    EditContact.Email = this.EditEmail.Text.Trim();
                    EditContact.PhoneNumber = this.EditMobile.Text.Trim();
                    EditContact.Birthday = isDate ? date : (DateTime?)null;

                    (ContactsWindow.GroupedContacts as ListCollectionView).CommitEdit();
                    (ContactsWindow.GroupedContacts as ListCollectionView).Refresh();
                }
                else
                {
                    var index = contactsWindow.preview.SelectedIndex;

                    string picture = "";
                    string fileName = System.IO.Path.GetFileName(EditContact.Picture);
                    if (fileName != "BusinessIcon.png" && fileName != "FamilyIcon.png" &&
                        fileName != "FriendIcon.png" && fileName != "OtherIcon.png")
                    {
                        picture = EditContact.Picture;
                    }

                    ContactFactory factory = new ContactFactory();
                    if (this.GroupType.SelectionBoxItem.ToString() == "Family")
                    {
                        EditContact = factory.CreateFamilyContact(this.EditName.Text.Trim(), this.EditMobile.Text.Trim(),
                            this.EditEmail.Text.Trim(), this.EditBirthday.Text.Trim());
                    }
                    else if (this.GroupType.SelectionBoxItem.ToString() == "Friends")
                    {
                        EditContact = factory.CreateFriendContact(this.EditName.Text.Trim(), this.EditMobile.Text.Trim(),
                            this.EditEmail.Text.Trim(), this.EditBirthday.Text.Trim());
                    }
                    else if (this.GroupType.SelectionBoxItem.ToString() == "Business")
                    {
                        EditContact = factory.CreateBusinessContact(this.EditName.Text.Trim(), this.EditMobile.Text.Trim(),
                            this.EditEmail.Text.Trim(), this.EditBirthday.Text.Trim());
                    }
                    else
                    {
                        EditContact = factory.CreateOtherContact(this.EditName.Text.Trim(), this.EditMobile.Text.Trim(),
                            this.EditEmail.Text.Trim(), this.EditBirthday.Text.Trim());
                    }

                    if (picture != "")
                    {
                        EditContact.Picture = picture;
                    }

                    (ContactsWindow.GroupedContacts as ListCollectionView).RemoveAt(index);
                    (ContactsWindow.GroupedContacts as ListCollectionView).AddNewItem(EditContact);
                    (ContactsWindow.GroupedContacts as ListCollectionView).CommitNew();
                }

                EditContact.CheckForBirthday();
                contactsWindow.preview.Items.Refresh();
                this.EditWindow.Close();
            }
            catch (InvalidDateException ex) { MessageBox.Show(ex.Message); }
            catch (InvalidPhoneException ex) { MessageBox.Show(ex.Message); }
            catch (AddressBookException ex) { MessageBox.Show(ex.Message); }
        }
    }
}
